package com.mason.service.user.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mason.service.user.UserService;
import com.mason.EnableOperationLog;
import com.mason.dao.UserDao;
import com.mason.model.UserDomain;
import com.mason.util.ModifyName;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author yunfei.shang
 * @title
 * @date 2018/05/24 11:19
 * @descreption
 */
@Service(value = "userService")
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    @Override
    public List<UserDomain> selectByPrimaryMap(Map mapKey) {
        return userDao.selectByPrimaryMap(mapKey);
    }

    @Override
    public UserDomain selectById(Map mapKey) {
        Integer id = Integer.parseInt(mapKey.get("id").toString());
        return userDao.selectById(id);
    }

    @Override
    @EnableOperationLog(name = ModifyName.SAVE,fieldName = {"id"},serviceclass = UserService.class)
    public int addUser(UserDomain user) {
        return userDao.insert(user);
    }

    /**
     * pageNum 开始页数
     * pageSize 每页显示的数据条数
     **/
    @Override
    public PageInfo<UserDomain> findAllUser(int pageNum, int pageSize) {
        //将参数传给这个方法就可以实现物理分页了，非常简单。
        PageHelper.startPage(pageNum, pageSize);
        List<UserDomain> userDomains = userDao.selectUsers();
        PageInfo result = new PageInfo(userDomains);
        return result;
    }

    @Override
    @EnableOperationLog(name = ModifyName.UPDATE,fieldName = {"id"},serviceclass = UserService.class,queryMethodName = "selectById")
    public int update(UserDomain user) {
        return userDao.update(user);
    }

    @Override
    @EnableOperationLog(name = ModifyName.UPDATE,fieldName = {"userName"},serviceclass = UserService.class)
    public int updateByName(UserDomain user) {
        return userDao.updateByName(user);
    }

    @Override
    @EnableOperationLog(name = ModifyName.UPDATE,fieldName = {"id"},serviceclass = UserService.class)
    public int updateByIds(List<Integer> idList) {
        return userDao.updateByIds(idList);
    }
}
